//
//  ShManageTools.m
//  YouTong
//
//  Created by chirui on 16/4/11.
//  Copyright © 2016年 chuirui_Shui. All rights reserved.
//

#import "ShManageTools.h"

#define FileFolder @"MYFILE" //存储文件的文件夹名
#define DOUCMENTS_PATH NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject

@implementation ShManageTools

-(instancetype)init{
    @throw [NSException exceptionWithName:@"Singletion" reason:@"不允许调用" userInfo:nil];
    return nil;
}

-(instancetype)initOther
{
    if (self=[super init]) {
        return self;
    }
    return self;
}

+(instancetype)shareModel{
    static ShManageTools *obj=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        obj=[[ShManageTools alloc]initOther];
    });
    return obj;
}

#pragma mark 存储文件data类型
+(BOOL)fileExistsFileName:(NSString *)file{
    NSFileManager *manger = [NSFileManager defaultManager];
    if([manger fileExistsAtPath:[NSString stringWithFormat:@"%@/%@/%@.archiver",DOUCMENTS_PATH,FileFolder,file]]){
        return YES;
    }else{
        return NO;
    }
}

//存
+ (void)fileSetFileName:(NSString *)name type:(id)type{
    NSFileManager *manger = [NSFileManager defaultManager];
    NSDictionary *dic = @{name:type};
    NSString *path = [NSString stringWithFormat:@"%@/%@",DOUCMENTS_PATH,FileFolder];
    NSLog(@"%@",path);
    if (![manger fileExistsAtPath:path]) {
        [manger createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
        NSLog(@"创建文件夹成功");
    }else{
        NSLog(@"文件夹已存在，无须重复创建");
    }
    //    if (![manger fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.archiver",path,name]]) {
    //       [manger createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@.archiver",path,name] withIntermediateDirectories:YES attributes:nil error:nil];
    //        NSLog(@"创建文件夹成功");
    //    }else{
    //        NSLog(@"文件夹已存在，无须重复创建");
    //    }
    //    UIImage *image = [UIImage imageNamed:@"icon"];
    //    NSData *data = UIImageJPEGRepresentation(image, 0.8);
    //    BOOL flag = [data writeToFile:[NSString stringWithFormat:@"%@/%@.archiver",path,name] atomically:YES];
    BOOL tag = [NSKeyedArchiver archiveRootObject:dic toFile:[NSString stringWithFormat:@"%@/%@.archiver",path,name]];
    NSLog(@"%@",[NSString stringWithFormat:@"%@/%@.archiver",path,name]);
    if (tag) {
        NSLog(@"文件存储成功");
    }else{
        NSLog(@"文件存储失败");
    }
}
//读
+ (id)fileGetFileName:(NSString *)name{
    NSFileManager *manger = [NSFileManager defaultManager];
    NSString *path = [NSString stringWithFormat:@"%@/%@/%@.archiver",DOUCMENTS_PATH,FileFolder,name];
    if ([manger fileExistsAtPath:path]) {
        NSDictionary *dic = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        NSLog(@"文件读取成功");
        return dic[name];
    }else{
        NSLog(@"文件不存在");
        return nil;
    }
}
//删
+ (void)fileRemoveByName:(NSString *)name{
    NSFileManager *manger = [NSFileManager defaultManager];
    NSString *path = [NSString stringWithFormat:@"%@/%@/%@.archiver",DOUCMENTS_PATH,FileFolder,name];
    if ([manger fileExistsAtPath:path]) {
        [manger removeItemAtPath:path error:nil];
        NSLog(@"删除单个文件成功");
    }else{
        NSLog(@"文件不存在");
    }
}
//删全部
+ (void)fileRemoveAllData{
    NSFileManager *manger = [NSFileManager defaultManager];
    NSString *path = [NSString stringWithFormat:@"%@/%@",DOUCMENTS_PATH,FileFolder];
    if ([manger fileExistsAtPath:path]) {
        [manger removeItemAtPath:path error:nil];
        NSLog(@"删除全部文件成功");
    }else{
        NSLog(@"文件夹不存在");
    }
}

+(float)fileSizeForDir//计算文件夹下文件的总大小

{
    
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSString * path = [NSString stringWithFormat:@"%@/%@",DOUCMENTS_PATH,FileFolder];
    float size =0;
    
    NSArray* array = [fileManager contentsOfDirectoryAtPath:path error:nil];
    
    for(int i = 0; i<[array count]; i++)
        
    {
        NSString *fullPath = [path stringByAppendingPathComponent:[array objectAtIndex:i]];
        
        BOOL isDir;
        
        if ( !([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && isDir) )
            
        {
            
            NSDictionary *fileAttributeDic=[fileManager attributesOfItemAtPath:fullPath error:nil];
            
            size+= fileAttributeDic.fileSize/ 1024.0/1024.0;
            
        }
        
        else
            
        {
            [self fileSizeForDir];
            
        }
        
    }
    
    return size;
    
}

@end
